package com.bjsxt.service.imp;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bjsxt.mapper.FuncsMapper;
import com.bjsxt.mapper.MenusMapper;
import com.bjsxt.mapper.RolesMapper;
import com.bjsxt.mapper.RolesMenusMapper;
import com.bjsxt.mapper.UsersMapper;
import com.bjsxt.pojo.Funcs;
import com.bjsxt.pojo.FuncsExample;
import com.bjsxt.pojo.Menus;
import com.bjsxt.pojo.MenusExample;
import com.bjsxt.pojo.Roles;
import com.bjsxt.pojo.RolesMenusExample;
import com.bjsxt.pojo.RolesMenusKey;
import com.bjsxt.pojo.Users;
import com.bjsxt.pojo.UsersExample;
import com.bjsxt.pojo.UsersExample.Criteria;
import com.bjsxt.service.UserLoginService;

import javassist.NotFoundException;
/**
 * 用户登陆处理service
 * @author 程应康
 *
 */
@Service
public class UserLoginServiceImpl implements UserLoginService {
	@Autowired
	private UsersMapper usersMapper;
	@Autowired
	private RolesMapper rolesMapper;
	@Autowired
	private RolesMenusMapper rolesMenuMapper;
	@Autowired
	private MenusMapper menusMapper;
	@Autowired
	private FuncsMapper funcsMapper;

	@Override
	public Users findUserByProperty(Users users) throws NotFoundException {
		UsersExample example = new UsersExample();
		RolesMenusExample rolesMenusExample = new RolesMenusExample();
		FuncsExample funcsExample = new FuncsExample();
		MenusExample menusExample = new MenusExample();
		Criteria criteria = example.createCriteria();
		criteria.andUsernameEqualTo(users.getUsername());
		criteria.andUserpwdEqualTo(users.getUserpwd());
		List<Users> uList = usersMapper.selectByExample(example);
		if (uList==null||uList.size()<=0) {
			throw new NotFoundException("用户名或者密码错误");
		}
		//获取user对象并创建属性
		Users user = uList.get(0);
		int roleid = user.getRoleid();
		List<Funcs> listFuncs = new ArrayList<>();
		List<Menus> listMenus = new ArrayList<>();
		
		//获取user的roles属性
		Roles role = this.rolesMapper.selectByPrimaryKey(roleid);
		user.setRoles(role);
		
		//根据roles属性中的roleid获取rolesMenus对象
		com.bjsxt.pojo.RolesMenusExample.Criteria criteriaRolesMenus = rolesMenusExample.createCriteria();
		criteriaRolesMenus.andRoleidEqualTo(roleid);
		List<RolesMenusKey> rolemenus = this.rolesMenuMapper.selectByExample(rolesMenusExample);
		
		//遍历rolesMenus对象
		for(RolesMenusKey d:rolemenus){
			//根据rolesMenus对象中的menusid去获取Menus表中Menu属性  并加入listMenus属性中去
			com.bjsxt.pojo.MenusExample.Criteria criteriaMenus = menusExample.createCriteria();
			criteriaMenus.andMenuidEqualTo(d.getMenuid());
			List<Menus> listmenu = this.menusMapper.selectByExample(menusExample);
			listMenus.addAll(listmenu);
			
			//根据rolesMenus对象中的menusid去获取Funcs表中的funcs属性  并加入到listFuncs中去
			com.bjsxt.pojo.FuncsExample.Criteria criteriafuncs = funcsExample.createCriteria();
			criteriafuncs.andMenuidEqualTo(d.getMenuid());
			List<Funcs> listfunc = this.funcsMapper.selectByExample(funcsExample);
			listFuncs.addAll(listfunc);
			System.out.println("遍历完成.........");
		}
		user.setFuns(listFuncs);
		user.setMenus(listMenus);
		return user;
	}

}
